﻿$(document).ready(function () {
    $(window).scroll(function () {
        var menu = document.querySelector("#main-header");
        var menulimit = menu.offsetTop + menu.offsetHeight;
        if ($(this).scrollTop() > menulimit) {
            $(".fixed-btns").fadeIn();
        }
        else {
            $(".fixed-btns").fadeOut();
        } 
    });
    $("#go-to-top-btn").click(function () {
        $("body,html").animate({ scrollTop: 0 }, 800);
        return false
    });
    $("#reply-btn").click(function () {
        $("#reply-form").fadeIn();
    });
    $("#close-reply-form-btn").click(function () {
        $("#reply-form").fadeOut();
    });
    $("#btn_send_Feedback").click(function () {
        var name = document.getElementById("Feedback_Fullname").value;
        var email = document.getElementById("Feedback_Email").value;
        var phone = document.getElementById("Feedback_Phone").value;
        var type = document.getElementById("Feedback_type").value;
        var content = $("#Feedback_Content").val();
        var captcha = document.getElementById("Feedback_Captcha").value;

        if (email.trim() == "") {
            document.getElementById("error-modal").style.display = "block";
            document.getElementById("error-modal").getElementsByClassName("error_content")[0].innerHTML = "Vui lòng nhập email";
            setTimeout("$('#error-modal').fadeOut()", 5000);
        }
        else {
            if (email.trim().indexOf("@") == -1 || email.trim().indexOf(".com") == -1) {
                document.getElementById("error-modal").style.display = "block";
                document.getElementById("error-modal").getElementsByClassName("error_content")[0].innerHTML = "Email đã nhập không đúng định dạng";
                setTimeout("$('#error-modal').fadeOut()", 5000);
            }
            else {
                if (type == 0) {
                    document.getElementById("error-modal").style.display = "block";
                    document.getElementById("error-modal").getElementsByClassName("error_content")[0].innerHTML = "Vui lòng chọn vấn đề cần góp ý.";
                    setTimeout("$('#error-modal').fadeOut()", 5000);
                }
                else {
                    if (content == "") {
                        document.getElementById("error-modal").style.display = "block";
                        document.getElementById("error-modal").getElementsByClassName("error_content")[0].innerHTML = "Vui lòng nhập nội dung cần góp ý.";
                        setTimeout("$('#error-modal').fadeOut()", 5000);
                    }
                    else {
                        if (captcha != getCookie("Captcha_Feedback")) {
                            reload_Captcha_Feedback();
                            document.getElementById("error-modal").style.display = "block";
                            document.getElementById("error-modal").getElementsByClassName("error_content")[0].innerHTML = "Mã xác nhận không chính xác";
                            setTimeout("$('#error-modal').fadeOut()", 5000);

                        }
                        else {
                            $.post("../ajax/asp/send_Feedback.aspx", { "name": name, "email": email, "phone": phone, "type": type, "content": content });
                            clear_reply_form();
                        }
                    }
                }
            }

        }

    });
    $("#reload_captcha_Feedback").click(function () {
        reload_Captcha_Feedback();
    });
    $("#btn_send_email").click(function () {
        var email = document.getElementById("txt_send_email").value;
        if (email.trim().indexOf("@") == -1) {
            document.getElementById("error-modal").style.display = "block";
            document.getElementById("error-modal").getElementsByClassName("error_content")[0].innerHTML = "Email không đúng định dạng";
            setTimeout("$('#error-modal').fadeOut()", 5000);
        }
        else {
            if (email.trim().indexOf(" ") != -1) {
                document.getElementById("error-modal").style.display = "block";
                document.getElementById("error-modal").getElementsByClassName("error_content")[0].innerHTML = "Email không đúng định dạng";
                setTimeout("$('#error-modal').fadeOut()", 5000);
            }
            else {
                if (email.trim().indexOf(".com") == -1) {
                    document.getElementById("error-modal").style.display = "block";
                    document.getElementById("error-modal").getElementsByClassName("error_content")[0].innerHTML = "Email không đúng định dạng";
                    setTimeout("$('#error-modal').fadeOut()", 5000);
                }
                else {
                    var xhttp;
                    xhttp = new XMLHttpRequest();
                    xhttp.onreadystatechange = function () {
                        if (xhttp.readyState == 4 && xhttp.status == 200) {
                            document.getElementById("check_email").innerHTML = xhttp.responseText;
                            var x = document.getElementById("check_email").innerHTML.trim();
                            if (x != "0") {
                                document.getElementById("error-modal").style.display = "block";
                                document.getElementById("error-modal").getElementsByClassName("error_content")[0].innerHTML = "Email này đã được đăng ký nhận tin";
                                setTimeout("$('#error-modal').fadeOut()", 5000);
                                document.getElementById("txt_send_email").value = "";
                            }
                            else {
                                $.post("../ajax/asp/send_email.aspx", { "email": email.trim() });
                                document.getElementById("success-modal").style.display = "block";
                                document.getElementById("success-modal").getElementsByClassName("success_content")[0].innerHTML = "Đăng ký nhận tin thành công!";
                                setTimeout("$('#success-modal').fadeOut()", 5000);
                                document.getElementById("txt_send_email").value = "";
                            }
                        }
                    };
                    xhttp.open("GET", "../ajax/asp/check_send_email.aspx?email="+email.trim(), true);
                    xhttp.send();
                }
            }
        }
    });
    $(".forget-pass-btn").click(function () {
        $("#forget-pass-modal").fadeIn();
        $(".forget-pass-input").focus();
    })
});
function open_collapse_menu() {
    var x = $("#collapse-menu");
    if (x.hasClass("uncollapse")) {
        x.animate({ "height": "toggle" }, "fast").removeClass("uncollapse");
        $(this).children().removeClass("fa-times").addClass("fa-bars");
        $(".collapse-menu-overlay").hide();
    } else {
        x.animate({ "height": "toggle" }, "fast").addClass("uncollapse");
        $(this).children().removeClass("fa-bars").addClass("fa-times");
        $(".collapse-menu-overlay").show();
    }
};
function drop_click(that) {
    var x = that.querySelector(".drop-target");
    if (x.className.indexOf("w3-show") == -1) {
        x.className += " w3-show";
    } else {
        x.className = x.className.replace(" w3-show", "");
    }
}
new WOW().init();
function getCookie(name) {
    var cname = name + "=";
    var dc = document.cookie;
    if (dc.length > 0) {
        begin = dc.indexOf(cname);
        if (begin != -1) {
            begin += cname.length;
            end = dc.indexOf(";", begin);
            if (end == -1) end = dc.length;
            return unescape(dc.substring(begin, end));
        }
    }
    return null;
}
function reload_Captcha_Feedback() {
    $(document).ready(function () {
        var xhttp;
        xhttp = new XMLHttpRequest();
        xhttp.onreadystatechange = function () {
            if (xhttp.readyState == 4 && xhttp.status == 200) {
                document.getElementById("captchaFeedback_area").innerHTML = xhttp.responseText;
            }
        };
        xhttp.open("GET", "../ajax/asp/loadcaptcha_Feedback.aspx", true);
        xhttp.send();
    });
}
function clear_reply_form() {
    $(document).ready(function () {
        document.getElementById("Feedback_Fullname").value = "";
        document.getElementById("Feedback_Email").value = "";
        document.getElementById("Feedback_Phone").value = "";
        document.getElementById("Feedback_type").value = 0;
        document.getElementById("Feedback_Content").value = "";
        document.getElementById("Feedback_Captcha").value = "";
        reload_Captcha_Feedback();
        $("#reply-form").fadeOut();
        document.getElementById("success-modal").style.display = "block";
        document.getElementById("success-modal").getElementsByClassName("success_content")[0].innerHTML = "Gửi tin thành công!<br/>Cảm ơn thông tin đóng góp của các bạn!";
        setTimeout("$('#success-modal').fadeOut()", 5000);
    });
}
function authentication() {
    document.getElementById("authentication-modal").style.display = "block";
}
function user_option() {
    var x = document.getElementById("user-setting");
    if (x.className.indexOf("w3-show") == -1) {
        x.className += " w3-show";
    } else {
        x.className = x.className.replace(" w3-show", "");
    }
}
function user_option_res() {
    var x = document.getElementById("user-setting-res");
    if (x.className.indexOf("w3-show") == -1) {
        x.className += " w3-show";
    } else {
        x.className = x.className.replace(" w3-show", "");
    }
}
function loginsignin(evt, action) {
    var i, x, tablinks;
    x = document.getElementsByClassName("action-content");
    for (i = 0; i < x.length; i++) {
        x[i].style.display = "none";
    }
    document.getElementById(action).style.display = "block";
}